Online Python Tutor

This notebook shows how to create a cell magic for calling Online Python Tutor.

First, we define the code to embed the tutor, and register the magic:

In [1]:
from IPython.display import HTML, display
from IPython.core.magic import register_line_cell_magic
import urllib

@register_line_cell_magic
def tutor(line, cell):
    code = urllib.urlencode({"code": cell})
    display(HTML("""
    <iframe width="800" height="500" frameborder="0"
            src="http://pythontutor.com/iframe-embed.html#%s&py=2">
    </iframe>
    """ % code))

Next, we write some code:

In [2]:
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

sum = 0
for i in range(len(matrix)):
    for j in range(len(matrix[i])):
        row = matrix[i]
        sum += row[j]
        print i, j
print sum
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
45

To see it execute line my line, we simply prefix the cell with our new %%tutor cell magic:

In [3]:
%%tutor
matrix = [[1, 2, 3], 
          [4, 5, 6], 
          [7, 8, 9]]

sum = 0
for i in range(len(matrix)):
    for j in range(len(matrix[i])):
        row = matrix[i]
        sum += row[j]
        print i, j
print sum